package utils.https;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;  
import org.apache.commons.httpclient.Header;  
import org.apache.commons.httpclient.HttpClient;  
import org.apache.commons.httpclient.HttpException;  
import org.apache.commons.httpclient.HttpStatus;  
import org.apache.commons.httpclient.methods.GetMethod;  
import org.apache.commons.httpclient.methods.PostMethod;  
import org.apache.commons.httpclient.methods.PutMethod;  
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;  
import org.apache.commons.logging.Log;  
import org.apache.commons.logging.LogFactory;  

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;

import play.Logger;
import utils.json.JSONUtils;
import constants.APIConstants;
import constants.Constants;

public class HttpRequest { 
	    private static HttpRequest httpRequst=null;     
	    private HttpRequest(){}    
	    public static HttpRequest getInstance(){  
	        if(httpRequst==null){  
	            synchronized(HttpRequest.class){  
	                if(httpRequst == null){  
	                    httpRequst=new HttpRequest();  
	                }  
	            }    
	        }  
	        return httpRequst;  
	    }  
	      
	    /** 
	     * HttpClient GET请求 
	     * @author huang 
	     * @date 2013-4-9 
	     * @param uri 
	     * @return resStr 请求返回的JSON数据 
	     */  
	    public String doGet(String uri,Map<String,Object> map) throws Exception{  
	        String resStr = null;  
	        HttpClient htpClient = new HttpClient();  
			String params = HttpUtils.map2MethodGetParams(map); 
			System.out.println(APIConstants.OPERATION_URL+uri+params);
	        GetMethod getMethod = new GetMethod(APIConstants.OPERATION_URL+uri+params);  
	        getMethod.getParams().setParameter( HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());       
	        try{  
	            int statusCode = htpClient.executeMethod( getMethod );  
//	            log.info(statusCode);  
	            if(statusCode != HttpStatus.SC_OK){                
	               Logger.info("Method failed: "+getMethod.getStatusLine());  
	                return resStr;  
	            }             
	            byte[] responseBody = getMethod.getResponseBody();           
	            resStr = new String(responseBody,Constants.HTTP_ENCODED);  
	        } catch (HttpException e) {  
	           Logger.info("Please check your provided http address!");  //发生致命的异常，可能是协议不对或者返回的内容有问题  
	        } catch (IOException e) {  
	           Logger.info( "Network anomaly" );  //发生网络异常  
	        }finally{  
	            getMethod.releaseConnection(); //释放连接  
	        }
	        return resStr;  
	    }  
	      
	    /** 
	     * HttpClient POST请求 
	     * @author huang 
	     * @date 2013-4-9 
	     * @param s_user 
	     * @return resStr 请求返回的JSON数据 
	     * @throws Exception 
	     */  
	    @SuppressWarnings( "deprecation" )  
	    public String doPost(String uri,Map<String, Object> map) throws Exception{      
	        String resStr = null;  
	        HttpClient htpClient = new HttpClient();  
	        PostMethod postMethod = new PostMethod(APIConstants.OPERATION_URL+uri);  
	        System.out.println(APIConstants.OPERATION_URL+uri);
	        postMethod.addRequestHeader("Content-Type","application/json" );    
	        postMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, Constants.HTTP_ENCODED ); 
	        String params=JSONUtils.getJsonObj(map);
	        System.out.println(params);
	        RequestEntity requestEntity = new StringRequestEntity(params,"text/xml","UTF-8");
	        postMethod.setRequestEntity(requestEntity);
	        try{  
	        	postMethod.releaseConnection();
	            int statusCode = htpClient.executeMethod( postMethod );  
//	            log.info(statusCode);  
	            if(statusCode != HttpStatus.SC_OK){  
	              //post和put不能自动处理转发          301：永久重定向，告诉客户端以后应从新地址访问    302：Moved Temporarily    
	                if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY||statusCode == HttpStatus.SC_MOVED_TEMPORARILY){  
	                     Header locationHeader = postMethod.getResponseHeader( "location" );  
	                     String location = null;  
	                    if(locationHeader!=null){  
	                        location = locationHeader.getValue();  
	                        Logger.info("The page was redirected to :"+location);  
	                    }else{  
	                        Logger.info("Location field value is null");  
	                    }  
	                }else{  
	                   Logger.info("Method failed: "+postMethod.getStatusLine());                      
	                }  
	                return resStr;  
	            }                       
	            byte[] responseBody = postMethod.getResponseBody();             
	            resStr = new String(responseBody,Constants.HTTP_ENCODED);    
	        }catch(Exception e){  
	            e.printStackTrace();  
	        }finally{  
	            postMethod.releaseConnection();  
	        }  
	        return resStr;  
	    }  
	      
	    /** 
	     * HttpClient PUT请求 
	     * @author huang 
	     * @date 2013-4-10 
	     * @return 
	     */  
	    @SuppressWarnings( "deprecation" )  
	    public String doPut(String uri,String jsonObj){  
	        String resStr = null;  
	        HttpClient htpClient = new HttpClient();  
	        PutMethod putMethod = new PutMethod(APIConstants.OPERATION_URL+uri);  
	        putMethod.addRequestHeader( "Content-Type","application/json" );  
	        putMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET,Constants.HTTP_ENCODED);  
	        putMethod.setRequestBody( jsonObj );  
	        try{  
	            int statusCode = htpClient.executeMethod( putMethod );  
//	            log.info(statusCode);  
	            if(statusCode != HttpStatus.SC_OK){  
	               Logger.info("Method failed: "+putMethod.getStatusLine());  
	                return null; 
	            }    
	            byte[] responseBody = putMethod.getResponseBody();           
	            resStr = new String(responseBody,Constants.HTTP_ENCODED);  
	        }catch(Exception e){  
	            e.printStackTrace();  
	        }finally{  
	            putMethod.releaseConnection();  
	        }  
	        return resStr;  
	    }  
}
